package com.credit.management.system.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Approve;
import com.credit.management.system.model.Page;
import com.credit.management.system.model.Project;
import com.credit.management.system.service.IApproveService;
import com.credit.management.system.service.IProjectService;

@Controller
@RequestMapping("/approve")
public class ApproveController {
	@Autowired
	private IApproveService approveService;
	@Autowired
	private IProjectService projectService;
	/**
	 * 进入审批表的页面
	 * 
	 */
	@RequestMapping("/approve")
	public String index(){
		return "approve/approve";
	}
	/**
	 * 添加Approve
	 * @param Approve
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Integer add(	Approve approve){
		Integer result=approveService.insertSelective(approve);
		return result;
	}
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/selectApproveById",method=RequestMethod.POST)
	@ResponseBody
	public Approve updatePre(Integer id){
		Approve approve=approveService.selectByPrimaryKey(id);
		return approve;
	}
	/**
	 * 修改Approve
	 * @param Approve
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Integer update(Approve approve){
		System.out.println("approve"+approve);
		//当审批项目为通过时将项目表状态改为1（通过），不通过是改为不通过
		if (approve.getApstate().equals("1")) {
			//根据id获取其项目id		
			Approve approve1 = approveService.selectByPrimaryKey(approve.getId());
			Project project = new Project();
			if (approve1 != null && approve1.getProjectid() != null) {
				project.setId(approve1.getProjectid());
				project.setState("1");
				projectService.updateByPrimaryKeySelective(project);
			} 
		}else if(approve.getApstate().equals("2")){
			Approve approve1 = approveService.selectByPrimaryKey(approve.getId());
			Project project = new Project();
			if (approve1 != null && approve1.getProjectid() != null) {
				project.setId(approve1.getProjectid());
				project.setState("2");
				projectService.updateByPrimaryKeySelective(project);
			} 
		}
		Integer result=approveService.updateByPrimaryKeySelective(approve);
		return result;
	}
	/**
	 * 根据id删除Approve
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Integer id){
		Integer result=0;
		
		result=approveService.deleteByPrimaryKey(id);
		return result;
	}
	/**
	 * 分页查询project，给datatables返回值, 未使用
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/approve_list_datatables",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> Approve_list_datatables(Integer start,Integer length){
		System.out.println("==========进入table=================");
		Map<String,Object> map=new HashMap<String,Object>();
		Page page1=approveService.findPageApproves(new Page(start,length));
		map.put("aaData", page1.getList());//查到的结果集
		map.put("recordsFiltered", page1.getTotal());//总条数，用于计算页数
		map.put("recordsTotal",  page1.getTotal());//总条数，用于页面显示：‘全部记录数  条’
		return map;
	}
	
	@RequestMapping(value="/approveRecords",method=RequestMethod.POST)
	/**
	 * 审批表的分页查询
	 * @param start
	 * @param length
	 * @return
	 */
	@ResponseBody
	public Map<String,Object> ApproveRecords(Integer start,Integer length){
		System.out.println("==========进入审批表分页的controller==table=================");
		Map<String,Object> map=new HashMap<String,Object>();
		Page page1=approveService.findPageAprrovesinfo(new Page(start,length));
		map.put("aaData", page1.getList());//查到的结果集
		map.put("recordsFiltered", page1.getTotal());//总条数，用于计算页数
		map.put("recordsTotal",  page1.getTotal());//总条数，用于页面显示：‘全部记录数  条’
		return map;
	}
	
}
